#include "stm32f10x.h"
#include "Encoder_Interface.h"

int16_t rpm;

void init_encoder_interface_in_tim3(void){

    
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
    
    TIM_InternalClockConfig(TIM3);
    
    GPIO_Init(GPIOA,&(GPIO_InitTypeDef){
        
        .GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7,
        .GPIO_Mode = GPIO_Mode_IN_FLOATING,
        .GPIO_Speed=GPIO_Speed_50MHz
    });
    /**
    Encoder interface mode acts simply as an external clock with direction selection. This
    means that the counter just counts continuously between 0 and the auto-reload value in the
    TIMx_ARR register (0 to ARR or ARR down to 0 depending on the direction). So the user
    must configure TIMx_ARR before starting. In the same way, the capture, compare,
    prescaler, trigger output features continue to work as normal
    */
    TIM_TimeBaseInit(TIM3,&(TIM_TimeBaseInitTypeDef){
        //  编码器接口的输入会先经过这里的分频
        // 所以如果这里设置的太大，那么ccr寄存器将很久不会得到更新
        .TIM_Prescaler= 0, 
        .TIM_CounterMode = TIM_CounterMode_Up, // 无效配置，计数器的行为被编码器接口接管
        .TIM_Period = 0xffff,       // 配置满量程计数
        .TIM_ClockDivision=TIM_CKD_DIV1
    
    });
    
    
 
  
    
    TIM_EncoderInterfaceConfig(
        TIM3,
        //both inputs are active on both rising and falling edges
        TIM_EncoderMode_TI12,
    /*
        这里配置极性和
        TIM_OC1PolarityConfig(TIM3,TIM_OCPolarity_Low);
        是一样的操作
    */
        TIM_ICPolarity_Rising,
        TIM_ICPolarity_Falling 
    );
    /*
    TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
    
    NVIC_Init(&(NVIC_InitTypeDef){
    
        .NVIC_IRQChannel = TIM2_IRQn,
        .NVIC_IRQChannelPreemptionPriority=1,
        .NVIC_IRQChannelSubPriority=2,
        .NVIC_IRQChannelCmd= ENABLE
    
    });
    
   
*/
    TIM_Cmd(TIM3,ENABLE);
}

/*

void TIM2_IRQHandler(void){

    if(TIM_GetITStatus(TIM2,TIM_IT_Update)){
    
        static int16_t last_count = 0;
//      利用补码的特性，可以很容易的把 计数器中的 0-65535 
        //      转换为  -32768 - 32767 之间
//          正数代表正转
//          负数代表反转        
        int16_t  count = TIM_GetCounter(TIM3);
        int16_t count_diff = count - last_count;
        last_count = count;
        // 旋转了多少圈
        // 因为经过配置的 proteus 中 encoder-motor 每转一圈包含 360 个脉冲
        // 所以除以360就能代表转了多少圈
        
        // 又因为这里 对于编码器A相的上升沿、下降沿和B相的上升沿、下降沿
        // 四个边沿都进行计数
        // 所以要把边沿数转换为脉冲数 
        // 所以要除以4
        
        
        rps rotation per second 每秒转速
         = 转过的圈数/ 所经过的时间
        
        因为所经过的时间为 0.01秒
        为了避免精度丢失，这里不用除以0.01 
        而是乘以 100
        
        rpm 为每分钟转多少圈
        rpm = 60 * rps
        
        
        // 计算过程参考：https://cloud.tencent.com/developer/article/1794018
        // M法测速 
        rpm = 60 * 100 * count_diff / 360/4;
        
        //encoder_overflow_count=0;
        
        
        
        // 每秒转多少圈
        //rotation_per_second = rotationCount;//1000;
        
        
        TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
    }

}
*/
